package com.gusha.service;


import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysFriendsDao;

import com.gusha.model.SysFriends;
import com.gusha.model.User;

@Service
@Transactional
public class SysFriendsService {
	@Autowired
    private SysFriendsDao sysFriendsDao;
	
    public Page<SysFriends> findAll(Pageable pageable, SysFriends sysFriends) {
    	
        @SuppressWarnings("serial")
        Page<SysFriends> result= sysFriendsDao.findAll(new Specification<SysFriends>() {
            @Override
            public Predicate toPredicate(Root<SysFriends> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (sysFriends.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),sysFriends.getId()));
                }
                if (sysFriends.getUser() != null) {
                    list.add(cb.equal(root.get("userId").as(User.class),sysFriends.getUser()));
                }
                if (sysFriends.getFriend() != null) {
                    list.add(cb.equal(root.get("friendId").as(User.class),sysFriends.getFriend()));
                }
             
                if (sysFriends.getState()!=null) {
                    list.add(cb.equal(root.get("state").as(BigInteger.class),sysFriends.getState()));
                }
                if (sysFriends.getIsDelete() != null) {
                    list.add(cb.equal(root.get("isDelete").as(BigInteger.class),sysFriends.getIsDelete()));
                }
               
                if (sysFriends.getAddTime() != null) {
                    list.add(cb.equal(root.get("addTime").as(Date.class),sysFriends.getAddTime()));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }
}
